package com.rybbaby.tss.front.tssMemberReturnPremium.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.annotation.validator.RegexpValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.consts.RoleConsts;
import com.rybbaby.tss.core.enums.MemberPropertyEnum;
import com.rybbaby.tss.core.enums.ReceiptTypeEnum;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.*;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssMealFee.entity.TssMealFee;
import com.rybbaby.tss.front.tssMealFee.service.ITssMealFeeService;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberReturnPremium.entity.TssMemberReturnPremium;
import com.rybbaby.tss.front.tssMemberReturnPremium.entity.ViewApprovalList;
import com.rybbaby.tss.front.tssMemberReturnPremium.entity.ViewRefundDetails;
import com.rybbaby.tss.front.tssMemberReturnPremium.service.*;
import com.rybbaby.tss.front.tssMemberReturnPremiumHistory.entity.TssMemberReturnPremiumHistory;
import com.rybbaby.tss.front.tssMemberReturnPremiumHistory.service.ITssMemberReturnPremiumHistoryService;
import com.rybbaby.tss.front.tssPayment.entity.TssPayment;
import com.rybbaby.tss.front.tssPayment.entity.ViewPaymentDetails;
import com.rybbaby.tss.front.tssPayment.entity.ViewPaymentReturnPremium;
import com.rybbaby.tss.front.tssPayment.service.ITssPaymentService;
import com.rybbaby.tss.front.tssPayment.service.IViewPaymentDetailsService;
import com.rybbaby.tss.front.tssPayment.service.IViewPaymentReturnPremiumService;
import com.rybbaby.tss.front.tssRightStatistic.service.ITssRightStatisticService;
import com.rybbaby.tss.front.tssStudent.entity.ViewStudentDetails;
import com.rybbaby.tss.front.tssStudent.service.IViewStudentDetailsService;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysAdminUser.service.ISysAdminUserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 会员退费记录表管理
 * 
 * @author 雷春琼
 * 
 * @createDate 2017-08-10 上午12:14:021
 */
@Controller
@RequestMapping(value = "tssMemberReturnPremiumController")
public class TssMemberReturnPremiumController extends BaseController {

	@Autowired
	private ITssMemberReturnPremiumService tssMemberReturnPremiumService;
	@Autowired
	private ITssMemberReturnPremiumHistoryService tssMemberReturnPremiumHistoryService;
	@Autowired
	private IViewRefundListService IViewRefundListService;
	@Autowired
	private IViewRefundDetailsService IViewRefundDetailsService;
	@Autowired
	private IViewPaymentDetailsService viewPaymentDetailsService;
	@Autowired
	private IViewPaymentReturnPremiumService viewPaymentReturnPremiumService;
	@Autowired
	private IViewMealFeeRefundService viewMealFeeRefundService;
	@Autowired
	private ITssMealFeeService tssMealFeeService;
	@Autowired
	private IViewStudentDetailsService viewStudentDetailsService;
	@Autowired
	private ViewApprovalListService viewApprovalListService;
	@Autowired
	private ITssRightStatisticService tssRightStatisticService;
	@Autowired
	private ITssMemberPackageService tssMemberPackageService;
	@Autowired
	private ISysAdminUserService sysAdminUserService;
	@Autowired
	private ITssPaymentService tssPaymentService;
	/**
	 * 获取会员退费记录表列表
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取会员退费记录表列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "tssMemberReturnPremiumList" , method = RequestMethod.POST)
	public ModelAndView tssMemberReturnPremiumList(TssMemberReturnPremium entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberReturnPremiumListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(TssMemberReturnPremium.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberReturnPremiumService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 收支记录-退费记录
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "收支记录-退费记录 " , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "ViewRefundDetailsList" , method = RequestMethod.POST)
	public ModelAndView ViewRefundDetailsList(ViewRefundDetails entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewRefundDetailsListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(ViewRefundDetails.class);
			//获取当前登录者
			String adminUserId = super.getLoginUser().getId();
			SysAdminUser user = super.getLoginUser();
			String ids = "";
			SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
			//获取当前登录者所拥有角色
			List<String> roleList = super.getLoginAdminRoles();

			if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
				dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + adminUserId + "') "));
			} else{
				List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
				ids = StringUtils.join(idsList, "','");
				dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + ids + "') "));
			}
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberReturnPremiumService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 获取会员可退费列表
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取会员可退费列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "ViewRefundList" , method = RequestMethod.GET)
	public ModelAndView ViewRefundList(TssMemberReturnPremium entity ,@RequestParam(required = false) String stuId) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewRefundListView"));
		try {
			//课时包可退款列表
			//List<ViewRefundList> packageRefundList = IViewRefundListService.packageRefundList(entity.getStuId());
			List<Map<String , Object>> packageRefundMap = tssMemberReturnPremiumService.packageRefundList(stuId);
			//定金可退款列表
			//List<ViewRefundList> depositRefundList = IViewRefundListService.depositRefundList(entity.getStuId());
			List<Map<String , Object>> depositRefundMap = tssMemberReturnPremiumService.depositRefundList(stuId);
			//其它缴费可退款列表
			//List<ViewRefundList> paymentRefundList = IViewRefundListService.paymentRefundList(entity.getStuId());
			List<Map<String , Object>> paymentRefundMap = tssMemberReturnPremiumService.paymentRefundList(stuId);
			//报名费可退款列表
			//List<ViewRefundList> registeryFeeRefundList = IViewRefundListService.registeryFeeRefundList(entity.getStuId());
			List<Map<String , Object>> registeryFeeRefundMap = tssMemberReturnPremiumService.registeryFeeRefundList(stuId);
			int index = 1;
			for (int i = 0; i < packageRefundMap.size(); i++) {
				packageRefundMap.get(i).put("index",index);
				index = index+1;
			}
			for (int i = 0; i < depositRefundMap.size(); i++) {
				depositRefundMap.get(i).put("index",index);
				index = index+1;
			}
			for (int i = 0; i < paymentRefundMap.size(); i++) {
				paymentRefundMap.get(i).put("index",index);
				index = index+1;
			}
			for (int i = 0; i < registeryFeeRefundMap.size(); i++) {
				registeryFeeRefundMap.get(i).put("index",index);
				index = index+1;
			}


			//mav.addObject("stuId" , entity.getMemberId());
			mav.addObject("stuId" , stuId);
			/*mav.addObject("packageRefundList" , packageRefundList);
			mav.addObject("paymentRefundList" , paymentRefundList);
			mav.addObject("depositRefundList" , depositRefundList);
			mav.addObject("registeryFeeRefundList" , registeryFeeRefundList);*/
			mav.addObject("packageRefundMap" , packageRefundMap);
			mav.addObject("depositRefundMap" , depositRefundMap);
			mav.addObject("paymentRefundMap" , paymentRefundMap);
			mav.addObject("registeryFeeRefundMap" , registeryFeeRefundMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	/**
	 * 会员申请退费
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "会员申请退费" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "toRefundApplication" , method = RequestMethod.POST)
	public ModelAndView toRefundApplication(@RequestParam(required = false) String paymentType,@RequestParam(required = false) String stuId
			,@RequestParam(required = false) String packageId,@RequestParam(required = false) String paymentId) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("toPackageRefundView"));
		mav.addObject("stuId" , stuId);
		mav.addObject("saveUrl" , StringUtils.substringBeforeLast(request.getRequestURI() , "/") + "/saveOrUpdate.do");
		try {
			if(paymentType != null && !"".equals(paymentType)){
				if("1".equals(paymentType) || "3".equals(paymentType)) {
					ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.packageRefundDetails(packageId);

					mav.addObject("data" , viewPaymentDetails);
					mav.addObject("packageId" , packageId);
					mav.addObject("refundType" , "1");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toPackageRefundView"));
				}
				if("2".equals(paymentType)) {
					ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.depositRefundDetails(paymentId);
					mav.addObject("data" , viewPaymentDetails);
					mav.addObject("refundType" , "2");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherRefundView"));
				}
				if("4".equals(paymentType)) {
					//餐点费
					TssMealFee tssMealFee = tssMealFeeService.findByStuId(stuId);
					//ViewMealFeeRefund viewMealFeeRefund = viewMealFeeRefundService.findByStuId(stuId);
					mav.addObject("data" , tssMealFee);
					//查询餐点费退费总额
					TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.findSumAmout(stuId,4);
					mav.addObject("sumAmout" , tssMemberReturnPremium.getSumAmout());
					mav.addObject("refundType" , "4");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toMealFeeRefundView"));
				}
				if("5".equals(paymentType)) {
					Map<String,Object> map = viewPaymentDetailsService.OtherRefundDetails(stuId,5);
					map.put("amount",map.get("sumAmount"));
					mav.addObject("data" , map);
					mav.addObject("refundType" , "5");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherRefundView"));
				}
				if("6".equals(paymentType)) {
					Map<String,Object> map =  viewPaymentDetailsService.OtherRefundDetails(stuId,6);
					map.put("amount",map.get("sumAmount"));
					mav.addObject("data" , map);
					mav.addObject("refundType" , "6");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherRefundView"));
				}
				if("7".equals(paymentType)) {
					ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.get(paymentId);
					mav.addObject("data" , viewPaymentDetails);
					mav.addObject("refundType" , "7");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toRegisteryFeeRefundView"));
				}
				/*if("8".equals(paymentType)) {
					ViewPaymentReturnPremium viewPaymentReturnPremium = viewPaymentReturnPremiumService.findByStuId(stuId);
					mav.addObject("data" , viewPaymentReturnPremium);
					mav.addObject("refundType" , "8");
					mav.setViewName(PropertiesReadUtil.getStringProperty("toSpecialRefundView"));
				}*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	/**
	 * 获取会员退费详情
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取会员退费详情" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "ViewRefundDetails" , method = RequestMethod.GET)
	public ModelAndView ViewRefundDetails(ViewRefundDetails entity,@RequestParam(required = false) String stuId) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewRefundDetailsView"));
		try {
			mav.addObject("stuId" , stuId);
			//用户信息
			ViewStudentDetails viewStudentDetails = viewStudentDetailsService.get(stuId);
			mav.addObject("viewStudentDetails" , viewStudentDetails);
			//退费记录
			List<ViewRefundDetails> viewRefundDetailsList = IViewRefundDetailsService.viewRefundDetailsList(stuId);

			mav.addObject("viewRefundDetailsList" , viewRefundDetailsList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	/**
	 * 获取单个退费详情
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取单个退费详情" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "refundDetail" , method = RequestMethod.GET)
	public ModelAndView refundDetail(ViewRefundDetails entity,@RequestParam(required = false) String stuId,@RequestParam(required = false) String refundId) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewRefundDetailsView"));
		try {
			mav.addObject("stuId" , stuId);
			//用户信息
			ViewStudentDetails viewStudentDetails = viewStudentDetailsService.get(stuId);
			mav.addObject("viewStudentDetails" , viewStudentDetails);
			//退费记录
			//List<ViewRefundDetails> viewRefundDetailsList = IViewRefundDetailsService.viewRefundDetailsList(stuId);

			ViewRefundDetails ViewRefundDetails = IViewRefundDetailsService.findByrefundId(refundId);

			mav.addObject("ViewRefundDetails" , ViewRefundDetails);
			//操作记录
			List<TssMemberReturnPremiumHistory> tssMemberReturnPremiumHistory = tssMemberReturnPremiumHistoryService.findByHistory(refundId);
			mav.addObject("tssMemberReturnPremiumHistory" , tssMemberReturnPremiumHistory);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	/**
	 * 获取审批列表
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取审批列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "approvalManagement" , method = RequestMethod.POST)
	public ModelAndView approvalManagement(ViewApprovalList entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewApprovalListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(ViewApprovalList.class);
			//获取当前登录者
			String adminUserId = super.getLoginUser().getId();
			SysAdminUser user = super.getLoginUser();
			String ids = "";
			SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
			//获取当前登录者所拥有角色
			List<String> roleList = super.getLoginAdminRoles();

			if(roleList.contains(RoleConsts.TSS_ROLE_KINDERGARTEN_LEADER)){
				List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
				ids = StringUtils.join(idsList, "','");
				/*dc.add(Restrictions.eq("refundStatus", 2));*/
				dc.add(Restrictions.sqlRestriction(" aadminUserId in ('" + ids + "') "));
			} else{
				dc.add(Restrictions.sqlRestriction(" createId in ('" + adminUserId + "') "));
				/*dc.add(Restrictions.sqlRestriction(" refundStatus != 2 "));*/
			}
			dc.addOrder(Order.desc("updateTime"));
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberReturnPremiumService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("addUrl" , requestURI + "/toAddedPaymentView.do");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 审批操作
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "审批操作" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "toApprovalOperation" , method = RequestMethod.POST)
	public ModelAndView toApprovalOperation(@RequestParam(required = false) String paymentType,@RequestParam(required = false) String stuId
			,@RequestParam(required = false) String packageId,@RequestParam(required = false) String paymentId,@RequestParam(required = false) String refundStatus
			,@RequestParam(required = false) String refundId,@RequestParam(required = false) String refundType,@RequestParam(required = false) String flag) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("toPackageApprovalView"));
		mav.addObject("stuId" , stuId);
		mav.addObject("saveUrl" , StringUtils.substringBeforeLast(request.getRequestURI() , "/") + "/saveOrUpdate.do");
		//查看详情
		if(flag!=null && !"".equals(flag)){
			if("1".equals(flag)){
				mav.addObject("modify" , "3");
			}
		}else{
			//审批流程控制页面显隐9
			if("3".equals(refundStatus)){
				mav.addObject("modify" , "2");
			}
			if("4".equals(refundStatus)){
				mav.addObject("modify" , "1");
			}
		}
		try {
			if(refundId != null && !"".equals(refundId)){
				//用户信息
				ViewStudentDetails viewStudentDetails = viewStudentDetailsService.get(stuId);
				BigDecimal sumAmountBig = new BigDecimal(viewStudentDetails.getSumAmount().toString());
				Double Total = sumAmountBig.subtract(new BigDecimal(viewStudentDetails.getRefundAmout().toString())).setScale(2, BigDecimal.ROUND_UP).doubleValue();
				viewStudentDetails.setTotal(Total);
				mav.addObject("viewStudentDetails" , viewStudentDetails);
				//操作记录
				List<TssMemberReturnPremiumHistory> tssMemberReturnPremiumHistoryList = tssMemberReturnPremiumHistoryService.findByProperty("returnPremiumId", refundId);
				List<TssMemberReturnPremiumHistory> tssMemberReturnPremiumHistory = tssMemberReturnPremiumHistoryService.findByHistory(refundId);
				mav.addObject("tssMemberReturnPremiumHistoryList" , tssMemberReturnPremiumHistoryList);
				mav.addObject("tssMemberReturnPremiumHistory" , tssMemberReturnPremiumHistory);

				if(refundType != null && !"".equals(refundType)) {
					if ("1".equals(refundType)) {
						//缴费详细信息
						ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.packageRefundDetails(packageId);
						mav.addObject("data", viewPaymentDetails);
						mav.addObject("packageId", packageId);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);

						mav.addObject("refundType" , "1");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toPackageApprovalView"));
					}
					if("2".equals(refundType)) {
						ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.depositRefundDetails(paymentId);
						mav.addObject("data" , viewPaymentDetails);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "2");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherApprovalView"));
					}
					if("4".equals(refundType)) {
						//餐点费
						TssMealFee tssMealFee = tssMealFeeService.findByStuId(stuId);
						//ViewMealFeeRefund viewMealFeeRefund = viewMealFeeRefundService.findByStuId(stuId);
						mav.addObject("data" , tssMealFee);
						//查询餐点费退费总额
						TssMemberReturnPremium sumAmout = tssMemberReturnPremiumService.findSumAmout(stuId,4);
						mav.addObject("sumAmout" , sumAmout.getSumAmout());
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "4");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toMealFeeApprovalView"));
					}
					if("5".equals(refundType)) {
						Map<String, Object> map = viewPaymentDetailsService.OtherRefundDetails(stuId, 5);
						map.put("amount",map.get("sumAmount"));
						mav.addObject("data" , map);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "5");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherApprovalView"));
					}
					if("6".equals(refundType)) {
						Map<String, Object> map = viewPaymentDetailsService.OtherRefundDetails(stuId,6);
						map.put("amount",map.get("sumAmount"));
						mav.addObject("data" , map);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "6");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherApprovalView"));
					}
					if("7".equals(refundType)) {
						ViewPaymentReturnPremium viewPaymentReturnPremium = viewPaymentReturnPremiumService.findByStuId(stuId);
						mav.addObject("data" , viewPaymentReturnPremium);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "7");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toOtherApprovalView"));
					}
					if("8".equals(refundType)) {
						ViewPaymentReturnPremium viewPaymentReturnPremium = viewPaymentReturnPremiumService.findByStuId(stuId);
						mav.addObject("data" , viewPaymentReturnPremium);
						//退费申请信息
						TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
						mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);
						mav.addObject("refundType" , "8");
						mav.setViewName(PropertiesReadUtil.getStringProperty("toSpecialApprovalView"));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	/**
	 * 审批详情
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "审批详情" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "approvalDetails" , method = RequestMethod.GET)
	public ModelAndView approvalDetails(ViewRefundDetails entity ,@RequestParam(required = false) String stuId,@RequestParam(required = false) String refundId) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewApprovalDetailsView"));
		try {
			//用户信息
			ViewPaymentReturnPremium viewPaymentReturnPremium = viewPaymentReturnPremiumService.get(stuId);
			mav.addObject("viewStudentDetails" , viewPaymentReturnPremium);
			//退费信息
			TssMemberReturnPremium tssMemberReturnPremium = tssMemberReturnPremiumService.get(refundId);
			mav.addObject("tssMemberReturnPremium" , tssMemberReturnPremium);

			//操作记录
			List<TssMemberReturnPremiumHistory> tssMemberReturnPremiumHistory = tssMemberReturnPremiumHistoryService.findByHistory(refundId);
			mav.addObject("tssMemberReturnPremiumHistory" , tssMemberReturnPremiumHistory);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
/*	*//**
	 * 保存审批
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 *//*
	@ResponseBody
	@RequestLog(content = "保存会员退费记录表对象" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "saveOrUpdateApproval" , method = RequestMethod.POST)
	public String saveOrUpdateApproval(TssMemberReturnPremium entity,@RequestParam(required = false) String stuId
			,@RequestParam(required = false) String approvalRemark,@RequestParam(required = false) String refundStatus
			,@RequestParam(required = false) String refundId) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			String id=entity.getId();
			ajr.setResMsg("保存成功!");
			if(StringUtils.isNotBlank(refundId)){
				TssMemberReturnPremium destEntity = this.tssMemberReturnPremiumService.get(refundId);
				//BeanUtils.copyProperties(entity , destEntity ,"createTime","updateTime");
				destEntity.setApprovalRemark(entity.getApprovalRemark());
				Integer newRefundStatus = Integer.parseInt("".equals(refundStatus)?"2":refundStatus);
				destEntity.setRefundStatus(newRefundStatus);
				destEntity.setApprovalRemark(approvalRemark);
				destEntity.setAdminUserId(adminUserId);
				destEntity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
				this.tssMemberReturnPremiumService.updateEntitiy(destEntity);
			} else {
				entity.setMemberId(stuId);
				entity.setCreateId(adminUserId);
				entity.setRefundType(1);
				entity.setRefundStatus(2);
				entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
				entity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
				Serializable newId = this.tssMemberReturnPremiumService.save(entity);
				if(newId == null) {
					ajr.setResCode(301);
					ajr.setResMsg("保存失败!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}*/

	/**
	 * 转到编辑视图
	 * 
	 * @param entity
	 *            接收编辑参数的临时对象
	 * @param serializeNum
	 *            请求标识
	 * @return
	 */
	@RequestLog(content = "转到会员退费记录表视图" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "toEditView" , method = RequestMethod.GET)
	public ModelAndView toEditView(TssMemberReturnPremium entity , @RequestParam(required = true) String serializeNum) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberReturnPremiumEditView"));
		if(entity != null && entity.getId() != null) {
			TssMemberReturnPremium editEntity = this.tssMemberReturnPremiumService.get(entity.getId());
			mav.addObject("editEntity" , editEntity);
			mav.addObject("editModel" , "edit");
		}else{
			mav.addObject("editModel" , "add");
		}
		mav.addObject("serializeNum" , serializeNum);
		mav.addObject("saveUrl" , StringUtils.substringBeforeLast(request.getRequestURI() , "/") + "/saveOrUpdate.do");
		return mav;
	}
	
	/**
	 * 保存编辑的对象
	 * 
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "保存会员退费记录表对象" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "saveOrUpdate" , method = RequestMethod.POST)
	public String saveOrUpdate(TssMemberReturnPremium entity,@RequestParam(required = false) String stuId
			,@RequestParam(required = false) String approvalRemark,@RequestParam(required = false) String refundStatus
			,@RequestParam(required = false) String refundId,@RequestParam(required = false) String classHour
			,@RequestParam(required = false) String amout,@RequestParam(required = false) String remark
			,@RequestParam(required = false) String isDelete,@RequestParam(required = false) Integer type
			,@RequestParam(required = false) Integer retirement,@RequestParam(required = false) String paymentId) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
			String adminUserId = super.getLoginUser().getId();
		Map<String, Object> schoolCode = sysAdminUserService.findSchoolCode(adminUserId);
		try {
			String id=entity.getId();
			ajr.setResMsg("保存成功!");
			if(StringUtils.isNotBlank(refundId)){
				TssMemberReturnPremium destEntity = this.tssMemberReturnPremiumService.get(refundId);
				//保存退费操作历史
				Serializable serializable = tssMemberReturnPremiumHistoryService.saveHistory(destEntity);
				//BeanUtils.copyProperties(entity , destEntity ,"createTime","updateTime");
				if(classHour!=null && !"".equals(classHour)){
					Integer newClassHour = Integer.parseInt("".equals(classHour)?"0":classHour);
					destEntity.setClassHour(newClassHour);
				}
				if(amout!=null && !"".equals(amout)){
					Double newAmout = Double.valueOf("".equals(amout)?"0.00":amout);
					destEntity.setAmout(newAmout);
				}
				if(remark!=null && !"".equals(remark)){
					destEntity.setRemark(remark);
				}
				if(isDelete!=null && !"".equals(isDelete)){
					Integer newIsDelete = Integer.parseInt("".equals(isDelete)?"0":isDelete);
					destEntity.setIsDelete(newIsDelete);

					/*//修改退费权责
					//查询权责
					TssRightStatistic tssRightStatistic = tssRightStatisticService.findByReturnPremiumId(destEntity.getId());
					boolean c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,null,adminUserId,null,null,newIsDelete,null);*/
				}
				if(refundStatus!=null){
					Integer newRefundStatus = Integer.parseInt("".equals(refundStatus)?"2":refundStatus);
					destEntity.setRefundStatus(newRefundStatus);
				}
				destEntity.setApprovalRemark(approvalRemark);
				destEntity.setAdminUserId(adminUserId);
				destEntity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
				this.tssMemberReturnPremiumService.updateEntitiy(destEntity);

				if(destEntity.getRefundStatus() == 5){
					//课时包退费权责
					HashMap<String, Object> map = new HashMap<>();
					Integer newType = 0;
					Integer responsibilityType = 0;
					if(destEntity.getRefundType() == 1){
						map.put("memberId", destEntity.getMemberId());
						map.put("returnPremiumId",destEntity.getId());
						TssMemberPackage tssMemberPackage = tssMemberPackageService.get(destEntity.getPackageId());
						//查询 课时包缴费详情
						ViewPaymentDetails viewPaymentDetails = viewPaymentDetailsService.packageRefundDetails(destEntity.getPackageId());
						Integer refundClassHour = destEntity.getClassHour();
						Double refundAmout = destEntity.getAmout();
						if(tssMemberPackage!=null){
							if(tssMemberPackage.getType() == 1){
								//设置权责类型
								newType = 5;
								responsibilityType = 9;
							}
							if(tssMemberPackage.getType() == 2){
								//设置权责类型
								newType = 6;
								responsibilityType = 10;
							}

							BigDecimal refundClassHourBigDecimal = new BigDecimal(refundClassHour);
							BigDecimal nowUnitPrice = new BigDecimal(viewPaymentDetails.getNowUnitPrice().toString()).setScale(2, BigDecimal.ROUND_UP);
							//退课时数 * 折后单价
							BigDecimal amountBigDecimal = nowUnitPrice.multiply(refundClassHourBigDecimal).setScale(2, BigDecimal.ROUND_UP);
							Double amountBigDouble = amountBigDecimal.doubleValue();
							Double amountResponsibility = 0.00;
							//小于 正权责
							if(refundAmout < amountBigDouble){
								amountResponsibility = amountBigDecimal.subtract(new BigDecimal(refundAmout)).setScale(2, BigDecimal.ROUND_UP).doubleValue();
								boolean c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(null,map,adminUserId,newType,responsibilityType,0,amountResponsibility);
							}
							if(refundAmout == amountBigDouble){

							}
							//大于 负权责
							if(refundAmout > amountBigDouble){
								amountResponsibility = amountBigDecimal.subtract(new BigDecimal(refundAmout)).setScale(2, BigDecimal.ROUND_UP).doubleValue();
								boolean c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(null,map,adminUserId,newType,responsibilityType,0,amountResponsibility);
							}
						}

						//清空冻结课时  转入已选班课时 已消耗课时
						if(tssMemberPackage.getType() ==1){
							tssMemberPackage.setSelectedClassHour(tssMemberPackage.getSelectedClassHour()+tssMemberPackage.getFreezeClassHour());
							tssMemberPackage.setConsumptionClassHour(tssMemberPackage.getConsumptionClassHour()+tssMemberPackage.getFreezeClassHour());
							tssMemberPackage.setPositiveConsumptionClassHour(tssMemberPackage.getPositiveConsumptionClassHour()+tssMemberPackage.getFreezeClassHour());
						}
						if(tssMemberPackage.getType() ==2){
							tssMemberPackage.setSelectedAttendanceDay(tssMemberPackage.getSelectedAttendanceDay()+tssMemberPackage.getFreezeClassHour());
							tssMemberPackage.setConsumptionAttendanceDay(tssMemberPackage.getConsumptionAttendanceDay()+tssMemberPackage.getFreezeClassHour());
							tssMemberPackage.setPositiveConsumptionAttendanceDay(tssMemberPackage.getPositiveUnusedAttendanceDay()+tssMemberPackage.getFreezeClassHour());
						}
						tssMemberPackage.setFreezeClassHour(0);
						tssMemberPackageService.updateMemberPackageEntitiy(tssMemberPackage);

					}
					//餐点费退费权责
					if(destEntity.getRefundType() == 4){
						TssMealFee tssMealFee = tssMealFeeService.findByStuId(destEntity.getMemberId());
						BigDecimal amount = new BigDecimal(tssMealFee.getAmount().toString()).setScale(2, BigDecimal.ROUND_UP);
						BigDecimal consumptionAmount = new BigDecimal(tssMealFee.getConsumptionAmount().toString()).setScale(2, BigDecimal.ROUND_UP);
						Double amountDouble = amount.subtract(consumptionAmount).setScale(2, BigDecimal.ROUND_UP).doubleValue();
						if(amountDouble < 0){
							map.put("memberId", destEntity.getMemberId());
							map.put("returnPremiumId",destEntity.getId());
							newType = 6;
							responsibilityType = 12;
							boolean c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(null,map,adminUserId,newType,responsibilityType,0,amountDouble);
						}

						//清空冻结餐点费  转入已消费金额
						BigDecimal freezeAmoutBig = new BigDecimal(tssMealFee.getFreezeAmout().toString());
						Double consumptionAmount1 = freezeAmoutBig.add(new BigDecimal(tssMealFee.getConsumptionAmount().toString()).setScale(2, BigDecimal.ROUND_UP)).doubleValue();

						tssMealFee.setConsumptionAmount(consumptionAmount1);
						tssMealFee.setFreezeAmout(0.00);
						tssMealFeeService.updateTssMealFee(tssMealFee);
					}
					//一次性物品费退费权责
					if(destEntity.getRefundType() == 5){
						map.put("memberId", destEntity.getMemberId());
						map.put("returnPremiumId",destEntity.getId());
						newType = 6;
						responsibilityType = 11;
						boolean c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(null,map,adminUserId,newType,responsibilityType,0,destEntity.getAmout());
					}
				}
				if(isDelete != null){
					if(isDelete.equals("1")){
						if(destEntity.getRefundType() == 1) {
							TssMemberPackage tssMemberPackage = tssMemberPackageService.get(destEntity.getPackageId());

							//清空冻结课时  转入剩余课时 剩余正课时
							if (tssMemberPackage.getType() == 1) {
								tssMemberPackage.setSurplusClassHour(tssMemberPackage.getSurplusClassHour() + tssMemberPackage.getFreezeClassHour());
								tssMemberPackage.setSurplusFormalClassHour(tssMemberPackage.getSurplusFormalClassHour() + tssMemberPackage.getFreezeClassHour());
								tssMemberPackage.setFreezeClassHour(0);
							}
							if (tssMemberPackage.getType() == 2) {
								tssMemberPackage.setSurplusAttendanceDay(tssMemberPackage.getSurplusAttendanceDay() + tssMemberPackage.getFreezeClassHour());
								tssMemberPackage.setSurplusFormalAttendanceDay(tssMemberPackage.getSurplusFormalAttendanceDay() + tssMemberPackage.getFreezeClassHour());
								tssMemberPackage.setFreezeClassHour(0);
							}
							tssMemberPackageService.updateMemberPackageEntitiy(tssMemberPackage);
						}
						if(destEntity.getRefundType() == 2) {
							//解冻订金
							TssPayment tssPayment = tssPaymentService.get(entity.getPaymentId());
							tssPayment.setDepositStatus(1);
							tssPaymentService.updateEntitiy(tssPayment);
						}
						if(destEntity.getRefundType() == 4) {
							//解冻餐点费
							TssMealFee tssMealFee = tssMealFeeService.findByStuId(destEntity.getMemberId());

							BigDecimal surplusAmoutBigDecimal = new BigDecimal(tssMealFee.getSurplusAmout());
							BigDecimal freezeAmoutBig = new BigDecimal(tssMealFee.getFreezeAmout());
							Double subtract = surplusAmoutBigDecimal.add(freezeAmoutBig).doubleValue();
							tssMealFee.setSurplusAmout(subtract);
							tssMealFee.setFreezeAmout(0.00);
							tssMealFeeService.updateTssMealFee(tssMealFee);
						}
					}
				}
			} else {

				//校验剩余餐点费是否够
				if(entity.getRefundType() == 4){
					TssMealFee tssMealFee = tssMealFeeService.findByStuId(stuId);
					BigDecimal data1 = new BigDecimal(tssMealFee.getSurplusAmout());
					BigDecimal data2 = new BigDecimal(entity.getAmout());
					Integer i = data1.compareTo(data2);
					if(i>=0){
						////餐点费退费冻结
						BigDecimal surplusAmoutBigDecimal = new BigDecimal(tssMealFee.getSurplusAmout());
						BigDecimal amoutBigDecimal = new BigDecimal(entity.getAmout());
						Double subtract = surplusAmoutBigDecimal.subtract(amoutBigDecimal).doubleValue();
						tssMealFee.setSurplusAmout(subtract);
						tssMealFee.setFreezeAmout(entity.getAmout());
						tssMealFeeService.updateTssMealFee(tssMealFee);
						//生成退费编码
						Map<String, Object> paramsMap = new HashMap<>();
						paramsMap.put("sequenceName", "SEQUENCE_TSS_RETURN_PREMIUM_CODE");
						paramsMap.put("schoolCode",schoolCode.get("schoolCode"));
						paramsMap.put("receiptType", ReceiptTypeEnum.退费.value());
						String newResultCode = CodeGenUtil.getCodeGen(paramsMap);
						Gson newGson = new Gson();
						Map<String,Object> map2 = newGson.fromJson(newResultCode, new TypeToken<Map<String,Object>>() {
						}.getType());
						if(Double.parseDouble(map2.get("resCode")+"")==200){
							entity.setRefundCode(map2.get("data").toString());
						}
						//保存餐点费退费信息
						entity.setIsDelete(0);
						entity.setMemberId(stuId);
						entity.setCreateId(adminUserId);
						entity.setAdminUserId(adminUserId);
						entity.setRefundStatus(2);
						entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
						entity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
						Serializable newId = this.tssMemberReturnPremiumService.save(entity);
						TssMemberReturnPremium newEntity = this.tssMemberReturnPremiumService.get(newId);
						Serializable serializable = tssMemberReturnPremiumHistoryService.saveHistory(newEntity);
						if(newId == null) {
							ajr.setResCode(301);
							ajr.setResMsg("保存失败!");
						}
					}
					if(i<0){
						ajr.setResMsg("餐点费不足!");
					}
				}
				if(entity.getRefundType() == 1){
					//检查课时包课时是否够用
					TssMemberPackage memberPackage = new TssMemberPackage();
					Integer sumClassHour = 0;
					memberPackage = tssMemberPackageService.get(entity.getPackageId());
					if(retirement == 0) {
						if (type == 1) {
							sumClassHour += memberPackage.getSurplusFormalClassHour();
						}
						if (type == 2) {
							sumClassHour += memberPackage.getSurplusFormalAttendanceDay();
						}
					}
					if(retirement == 1){
						if (type == 1) {
							sumClassHour += memberPackage.getSurplusFormalClassHour();
							sumClassHour += memberPackage.getPositiveUnusedClassHour();
						}
						if (type == 2) {
							sumClassHour += memberPackage.getSurplusFormalAttendanceDay();
							sumClassHour += memberPackage.getPositiveUnusedAttendanceDay();
						}
					}
					if(sumClassHour >= entity.getClassHour()){
						//一键退班

						//冻结课时包所退课时
						if(type == 1){
							Integer surplusFormalClassHour = memberPackage.getSurplusFormalClassHour();
							surplusFormalClassHour = surplusFormalClassHour - entity.getClassHour();
							memberPackage.setSurplusFormalClassHour(surplusFormalClassHour);

							Integer surplusClassHour = memberPackage.getSurplusClassHour();
							surplusClassHour = surplusClassHour - entity.getClassHour();
							memberPackage.setSurplusFormalClassHour(surplusClassHour);
						}
						if(type == 2){
							Integer surplusFormalAttendanceDay = memberPackage.getSurplusFormalAttendanceDay();
							surplusFormalAttendanceDay = surplusFormalAttendanceDay - entity.getClassHour();
							memberPackage.setSurplusFormalAttendanceDay(surplusFormalAttendanceDay);

							Integer surplusAttendanceDay = memberPackage.getSurplusAttendanceDay();
							surplusAttendanceDay = surplusAttendanceDay - entity.getClassHour();
							memberPackage.setSurplusAttendanceDay(surplusAttendanceDay);
						}
						memberPackage.setFreezeClassHour(entity.getClassHour());
						tssMemberPackageService.updateMemberPackageEntitiy(memberPackage);

						//生成退费编码
						Map<String, Object> paramsMap = new HashMap<>();
						paramsMap.put("sequenceName", "SEQUENCE_TSS_RETURN_PREMIUM_CODE");
						paramsMap.put("schoolCode",schoolCode.get("schoolCode"));
						paramsMap.put("receiptType", ReceiptTypeEnum.退费.value());
						String newResultCode = CodeGenUtil.getCodeGen(paramsMap);
						Gson newGson = new Gson();
						Map<String,Object> map2 = newGson.fromJson(newResultCode, new TypeToken<Map<String,Object>>() {
						}.getType());
						if(Double.parseDouble(map2.get("resCode")+"")==200){
							entity.setRefundCode(map2.get("data").toString());
						}
						//保存课时包退费信息
						entity.setIsDelete(0);
						entity.setMemberId(stuId);
						entity.setCreateId(adminUserId);
						entity.setAdminUserId(adminUserId);
						entity.setRefundStatus(2);
						entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
						entity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
						Serializable newId = this.tssMemberReturnPremiumService.save(entity);
						TssMemberReturnPremium newEntity = this.tssMemberReturnPremiumService.get(newId);
						Serializable serializable = tssMemberReturnPremiumHistoryService.saveHistory(newEntity);
						if(newId == null) {
							ajr.setResCode(301);
							ajr.setResMsg("保存失败!");
						}
					}else {
						ajr.setResMsg("课时不足!");
					}

				}
				if(entity.getRefundType() == 2){
					TssPayment tssPayment = tssPaymentService.get(entity.getPaymentId());
					tssPayment.setDepositStatus(2);
					tssPaymentService.updateEntitiy(tssPayment);
				}
				if(entity.getRefundType() != 1 &&entity.getRefundType() !=4) {
					entity.setMemberId(stuId);
					if(entity.getRefundType()==5 || entity.getRefundType()==6){
						entity.setPaymentId(null);
					}
					entity.setCreateId(adminUserId);
					entity.setAdminUserId(adminUserId);
					entity.setRefundStatus(2);
					//生成退费编码
					Map<String, Object> paramsMap = new HashMap<>();
					paramsMap.put("sequenceName", "SEQUENCE_TSS_RETURN_PREMIUM_CODE");
					paramsMap.put("schoolCode",schoolCode.get("schoolCode"));
					paramsMap.put("receiptType", ReceiptTypeEnum.退费.value());
					String newResultCode = CodeGenUtil.getCodeGen(paramsMap);
					Gson newGson = new Gson();
					Map<String,Object> map2 = newGson.fromJson(newResultCode, new TypeToken<Map<String,Object>>() {
					}.getType());
					if(Double.parseDouble(map2.get("resCode")+"")==200){
						entity.setRefundCode(map2.get("data").toString());
					}
					entity.setIsDelete(0);
					entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
					entity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
					Serializable newId = this.tssMemberReturnPremiumService.save(entity);
					TssMemberReturnPremium newEntity = this.tssMemberReturnPremiumService.get(newId);
					Serializable serializable = tssMemberReturnPremiumHistoryService.saveHistory(newEntity);
					if(newId == null) {
						ajr.setResCode(301);
						ajr.setResMsg("保存失败!");
					}
				}
				//课时包退费冻结



			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	
	/**
	 * 删除指定id的对象
	 * 
	 * @param id
	 *            需要被删除的对象id
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "删除属性" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "delete" , method = RequestMethod.GET)
	public String delete(@StringValidator(nullable = false) String id) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			ajr.setResMsg("删除成功!");
			if(StringUtils.isNotBlank(id)) {
				this.tssMemberReturnPremiumService.deleteEntityById(id);
			} else {
				ajr.setResMsg("删除失败!");
				ajr.setResCode(302);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	
	/**
	 * 批量删除指定id的对象
	 * 
	 * @param ids
	 *            需要被删除的对象id组成的字符串
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "删除属性" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "batchDelete" , method = RequestMethod.GET)
	public String batchDelete(@RegexpValidator(nullable = false , regexp = "^(\\w+,?)+\\w+$") String ids) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			ajr.setResMsg("删除成功!");
			if(StringUtils.isNotBlank(ids)) {
				boolean batchDelete = this.tssMemberReturnPremiumService.batchDelete(ids.split(","));
				if(!batchDelete) {
					ajr.setResMsg("删除失败!");
					ajr.setResCode(302);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}



	@RequestLog(content = "导出", save = true)
	@Token(validateToken = false)
	@ResponseBody
	@RequestMapping("/exportExcle")
	public void exportExcle(Integer flag, ViewApprovalList entity, HttpServletResponse response, HttpServletRequest request){
		String path = "jso/export_approval.json";
		List<ViewApprovalList> classesList = null;
		String templateName = request.getParameter("templateName");
		if(flag == 0){
			classesList = viewApprovalListService.getAllList();
		}

        if(flag == 1){}

		ExportUtil e = new ExportUtil();
        e.export(path, templateName, classesList, request, response);

	}

}
